<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #pig {
        position: absolute;
      }
    </style>
  </head>
  <body>
    <img src="./pig.jpg" width="50px" id="pig" alt="" />
    <script>
      var pigEle = document.getElementById('pig');
      // 给文档对象绑定单击事件
      document.addEventListener('click', function (event) {
        // 获取xy点击的坐标
        var x = event.clientX;
        var y = event.clientY;
        // 把xy坐标赋值给图片的left和top偏移量
        pigEle.style.left = x + 'px';
        pigEle.style.top = y + 'px';
      });
    </script>
  </body>
</html>
